package annotationLearn;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * TestAnnotation
 *
 * @author nanxin.chen
 * @date 2015/10/31
 */
public class CNAnnotationTest {
    public static void main(String[] args) {

        Class c = CNAnnotationTest.class;
        try {
            System.out.println("getDeclaredMethods................");
            for (Method m : c.getDeclaredMethods()) {
                System.out.println(m.getName());
            }
            System.out.println("getMethods....................");
            for (Method m : c.getMethods()) {
                System.out.println(m.getName());
            }
            System.out.println("getAnnotation................");
            Method m = c.getDeclaredMethod("printInfo",String.class,int.class);
            Annotation anno[] = m.getDeclaredAnnotations();
            for (Annotation annotation : anno) {
                System.out.println(annotation.annotationType().getName());
            }
            System.out.println("use annotation to execute methed.......");
            CNAnnotation cna = m.getAnnotation(CNAnnotation.class);
            m.invoke(c.newInstance(), cna.name(), cna.gender().getNum());
            m = c.getDeclaredMethod("printInfo2", String.class, int.class);
            cna = m.getAnnotation(CNAnnotation.class);
            m.invoke(c.newInstance(), cna.name(), cna.gender().getNum());

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("sheet !!!");
        }
    }


    @CNAnnotation(name = "testcn1", gender = GenderEnum.WOMAN)
    public void printInfo(String name, int gender) {
        System.out.println(name + " " + GenderEnum.getGender(gender));
    }

    @CNAnnotation
    public void printInfo2(String name, int gender) {
        System.out.println(name + " " + GenderEnum.getGender(gender));
    }
}
